<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="采集时间" prop="ccjsj">
        <el-date-picker clearable
          v-model="queryParams.ccjsj"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择采集时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="区域编号" prop="szqy">
        <el-input
          v-model="queryParams.szqy"
          placeholder="请输入区域编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="系统名称" prop="xtmc">
        <el-input
          v-model="queryParams.xtmc"
          placeholder="请输入系统名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="单位名称" prop="dwmc">
        <el-input
          v-model="queryParams.dwmc"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="所在地址" prop="szdz">
        <el-input
          v-model="queryParams.szdz"
          placeholder="请输入所在地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="创建时间" prop="cjsj">
        <el-input
          v-model="queryParams.cjsj"
          placeholder="请输入创建时间"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="操作" prop="cz">
        <el-input
          v-model="queryParams.cz"
          placeholder="请输入操作"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="archiveCode" prop="archiveCode">
        <el-input
          v-model="queryParams.archiveCode"
          placeholder="请输入archiveCode"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="approvalReason" prop="approvalReason">
        <el-input
          v-model="queryParams.approvalReason"
          placeholder="请输入approvalReason"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:xxxtsimp:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:xxxtsimp:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:xxxtsimp:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:xxxtsimp:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="xxxtsimpList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="区域编号" align="center" prop="szqy" />
      <el-table-column label="采集时间" align="center" prop="ccjsj" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.ccjsj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="系统名称" align="center" prop="xtmc" />
      <el-table-column label="单位名称" align="center" prop="dwmc" />
      <el-table-column label="所在地址" align="center" prop="szdz" />
      <el-table-column label="创建时间" align="center" prop="cjsj" />
      <el-table-column label="操作" align="center" prop="cz" />
      <el-table-column label="archiveCode" align="center" prop="archiveCode" />
      <el-table-column label="approvalReason" align="center" prop="approvalReason" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleDetail(scope.row)" v-hasPermi="['manage:xxxtsimp:edit']">查看详情</el-button>
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:xxxtsimp:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:xxxtsimp:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改信息系统基础信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="xxxtsimpRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="采集时间" prop="ccjsj">
          <el-date-picker clearable
            v-model="form.ccjsj"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择采集时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="系统名称" prop="xtmc">
          <el-input v-model="form.xtmc" placeholder="请输入系统名称" />
        </el-form-item>
        <el-form-item label="单位名称" prop="dwmc">
          <el-input v-model="form.dwmc" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="所在地址" prop="szdz">
          <el-input v-model="form.szdz" placeholder="请输入所在地址" />
        </el-form-item>
        <el-form-item label="创建时间" prop="cjsj">
          <el-input v-model="form.cjsj" placeholder="请输入创建时间" />
        </el-form-item>
        <el-form-item label="操作" prop="cz">
          <el-input v-model="form.cz" placeholder="请输入操作" />
        </el-form-item>
        <el-form-item label="archiveCode" prop="archiveCode">
          <el-input v-model="form.archiveCode" placeholder="请输入archiveCode" />
        </el-form-item>
        <el-form-item label="approvalReason" prop="approvalReason">
          <el-input v-model="form.approvalReason" placeholder="请输入approvalReason" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

        <!-- 详情对话框 -->
        <el-dialog :title="viewTitle" :inline="true" v-model="viewOpen" width="1200px" append-to-body>
          <el-form ref="xxxtsimpRef" :model="form" :rules="rules" label-width="220px">
            <el-form-item label="创建时间">
              <div v-text="viewForm.cjsj"></div>
            </el-form-item>
            <el-form-item label="系统名称">
              <div v-text="viewForm.xtmc"></div>
            </el-form-item>
            <el-form-item label="单位名称">
              <div v-text="viewForm.dwmc"></div>
            </el-form-item>
            <el-form-item label="所在地址">
              <div v-text="viewForm.szdz"></div>
            </el-form-item>
            <el-form-item label="操作" >
              <div v-text="viewForm.cz"></div>
            </el-form-item>
            <el-form-item label="备注">
              <div v-text="viewForm.approvalReason"></div>
            </el-form-item>
            <el-divider content-position="center">系统备案信息</el-divider>
            <el-form-item label="备案编号">
              <div v-text="viewForm.babh"></div>
            </el-form-item>
            <el-form-item label="区域编号">
              <div v-text="viewForm.szqy"></div>
            </el-form-item>
            <el-form-item label="统一社会信用代码">
              <div v-text="viewForm.socialCreditCode"></div>
            </el-form-item>
            <el-form-item label="系统所在地址">
              <div v-text="viewForm.locationAddress"></div>
            </el-form-item>
            <el-form-item label="街道/乡镇">
              <div v-text="viewForm.subDistrict"></div>
            </el-form-item>
            <el-form-item label="派出所编码">
              <div v-text="viewForm.affiliatedUnitCode"></div>
            </el-form-item>
            <el-form-item label="派出所名称">
              <div v-text="viewForm.affiliatedUnitName"></div>
            </el-form-item>
            <el-form-item label="公共安全区域图像采集设备数量">
              <div v-text="viewForm.publicDeviceCount"></div>
            </el-form-item>
            <el-form-item label="安全负责人姓名">
              <div v-text="viewForm.leaderName"></div>
            </el-form-item>
            <el-form-item label="安全负责人电话">
              <div v-text="viewForm.leaderTelephone"></div>
            </el-form-item>
            <el-form-item label="经办人姓名">
              <div v-text="viewForm.transactor"></div>
            </el-form-item>
            <el-form-item label="经办人身份证号码">
              <div v-text="viewForm.transactorId"></div>
            </el-form-item>
            <el-form-item label="点位列表">
              <div v-if="viewForm.pointInfo == null">无点位清单</div>
              <div v-if="viewForm.pointInfo != null">
                <el-button @click="downloadExcel">下载 Excel</el-button>
              </div>
            </el-form-item>
            <el-divider content-position="center">单位详情信息</el-divider>
            <el-form-item label="营业执照/统一社会信用代码证书复印件加盖公章" style="height: 300px;" >
              <el-image :src="viewForm.yyzz" style="height: 300px; width: auto;" fit="contain" ></el-image>
            </el-form-item>
            <el-form-item label="法人登记证书复印件加盖公章" style="height: 300px;" >
              <el-image :src="viewForm.frdjzs" style="height: 300px; width: auto;" fit="contain" ></el-image>
            </el-form-item>
            <el-form-item label="委托办理备案登记授权书加盖公章" style="height: 300px;" >
              <el-image :src="viewForm.sqs" style="height: 300px; width: auto;" fit="contain" ></el-image>
            </el-form-item>
          </el-form>
          <template #footer>
        <div class="dialog-footer">
          <el-button @click="viewOpen = false">关 闭</el-button>
        </div>
      </template>
        </el-dialog>


  </div>
</template>

<script setup name="Xxxtsimp">
import { listXxxtsimp, getXxxtsimp, delXxxtsimp, addXxxtsimp, updateXxxtsimp } from "@/api/manage/xxxtsimp";

const { proxy } = getCurrentInstance();

const xxxtsimpList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    ccjsj: null,
    xtmc: null,
    dwmc: null,
    szdz: null,
    cjsj: null,
    cz: null,
    archiveCode: null,
    approvalReason: null,
    szqy: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);


function base64ToBlob(base64) {
  // 去除 data URL 前缀（如果有的话）
  const base64Data = base64.split(',')[1] || base64;
  const byteCharacters = atob(base64Data);
  const byteNumbers = new Array(byteCharacters.length);
  for (let i = 0; i < byteCharacters.length; i++) {
    byteNumbers[i] = byteCharacters.charCodeAt(i);
  }
  const byteArray = new Uint8Array(byteNumbers);
  return new Blob([byteArray], { type: 'application/vnd.ms-excel' }); // .xls 类型
}

function downloadExcelFromBase64(base64String, filename = '文件.xls') {
  const blob = base64ToBlob(base64String);
  const url = window.URL.createObjectURL(blob);

  const a = document.createElement('a');
  a.href = url;
  a.download = filename;
  document.body.appendChild(a);
  a.click();

  // 清理资源
  window.URL.revokeObjectURL(url);
  document.body.removeChild(a);
}


const downloadExcel = () => {
  if (!viewForm.value.pointInfo) {
    proxy.$modal.msgError("没有可下载的文件");
    return;
  }

  downloadExcelFromBase64(viewForm.value.pointInfo, viewForm.value.xtmc+'.xls');
};


/** 查询信息系统基础信息列表 */
function getList() {
  loading.value = true;
  listXxxtsimp(queryParams.value).then(response => {
    xxxtsimpList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

const viewOpen = ref(false);
const viewTitle = ref("");
const viewForm = ref({});

// 查看巡检细节
function handleDetail(row){
  reset();
  const _id = row.id || ids.value
  getXxxtsimp(_id).then(response => {
    viewForm.value = response.data;
    viewOpen.value = true;
    viewTitle.value = "详细信息";
  });
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    createTime: null,
    ccjsj: null,
    xtmc: null,
    dwmc: null,
    szdz: null,
    cjsj: null,
    cz: null,
    archiveCode: null,
    approvalReason: null
  };
  proxy.resetForm("xxxtsimpRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加信息系统基础信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getXxxtsimp(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改信息系统基础信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["xxxtsimpRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateXxxtsimp(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addXxxtsimp(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除信息系统基础信息编号为"' + _ids + '"的数据项？').then(function() {
    return delXxxtsimp(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/xxxtsimp/export', {
    ...queryParams.value
  }, `xxxtsimp_${new Date().getTime()}.xlsx`)
}

getList();
</script>
